# -*- coding: utf-8 -*-
import os
import sys
from flask import Flask, redirect,request, url_for, send_from_directory,Response,render_template
# from pygame import Cursor
from werkzeug.utils import secure_filename
from PIL import Image
import shutil
import json
import time
from time import sleep
import pymysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='root', database='calender', charset='utf8')
cur = conn.cursor()
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif','txt'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'templates'
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
app.config['JSON_AS_ASCII']=False
import jieba



@app.route('/',methods=['POST','GET'])
def y():
    if  request.method == 'POST':
        print(234567)
        print(request.form)
        print(request.args)
    return render_template('search.html')


def sql_query(keyword):
    sql= "select * from normal where details like '%{keyword}%' or title like '%{keyword}%'".format(keyword=keyword)
    cur.execute(sql)
    result=cur.fetchall()
    return result
 


@app.route('/search',methods=['POST','GET'])
def yy():
    if  request.method == 'POST':
        
        print(4555555555)
        print(request.form)
        keyword=request.form.get('keyword')
        cut_keywords = jieba.cut_for_search(keyword)
        search_result = []
        # 取出待搜索keyword
        keyword = request.form['keyword']
        # 对keyword分词
        cut_keywords = jieba.cut_for_search(keyword)
        for cut_keyword in cut_keywords:
            # print(cut_keyword)
            print(sql_query(cut_keyword))

    return render_template('search.html')








if __name__ == '__main__':
    app.jinja_env.auto_reload=True
    app.config['TEMPLATES_AUTO_RELOAD'] =True
    app.run(host='0.0.0.0',port=7000,debug=True)

